package com.young.jpademo2.controller;

import com.young.jpademo2.rsa.RsaTest;
import com.young.jpademo2.rsa1.RsaTest1;
import org.apache.commons.codec.binary.Base64;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;

@RestController
public class RsaController {
    @RequestMapping(value = "/api/encryptTest",method = RequestMethod.GET)
    @ResponseBody
    public Object test(@RequestParam(value = "encryptData",required = true) String encryptData) throws Exception {
        System.out.println("——————————————————进入到了Controller——————————————————");
        RsaTest rsaTest=new RsaTest();
        //rsaTest.createKey();
        String data=encryptData;
        //客户端加密
        byte[] bytes=rsaTest.webEncrypt(data);
        Map map=new HashMap();
        map.put("code",200);
        //map.put("message","加密成功！,加密后的数据为："+ bytes);
        map.put("message","加密成功！,加密后的数据为："+ new String(Base64.encodeBase64(bytes)));
        //服务端解密
        String result=rsaTest.serverDecr(bytes);
        RsaTest1 rsaTest1=new RsaTest1();
        //rsaTest1.createKey();
        //服务端加密
        byte[] bytes2=rsaTest1.serEncrypt(result);
        //服务端再解密
        rsaTest1.serverDecr1(bytes2);
        return map;
        /*Map map=new HashMap();
        map.put("code",200);
        map.put("message","加密成功！,服务端已处理");
        return map;*/
    }
}
